package dataStructureAlgorithms;

/**
 * @Description: TODO 数组实现的队列
 * @Author: wzj
 * @Date: 2019/10/8 7:56
 */
public class ArrayQueue {
    int[] queue;

    public ArrayQueue() {
        queue=new int[0];
    }

    /**
     * 添加到队列
     * @param value
     */
    public void add(int value){
        int[] queueNew=new int[queue.length+1];
        for(int i=0;i<queueNew.length-1;i++){
            queueNew[i]=queue[i];
        }
        queueNew[queue.length]=value;
        queue=queueNew;
    }

    /**
     * 返回并移除队头元素
     */
    public int poll(){
        if(queue.length==0){
            throw new RuntimeException("队列为空");
        }
        int head=queue[0];
        int[] queueNew=new int[queue.length-1];
        for(int i=0;i<queue.length-1;i++){
            queueNew[i]=queue[i+1];
        }
        queue = queueNew;
        return head;
    }

    /**
     * 查看队头元素
     * @return
     */
    public int peek(){
        if(queue.length==0){
            throw new RuntimeException("队列为空");
        }
        return queue[0];
    }

    /**
     * 判断是否为空
     * @return
     */
    public boolean isEmpty(){
        return queue.length==0;
    }

    public static void main(String[] args) {
        ArrayQueue arrayQueue=new ArrayQueue();
        System.out.println(arrayQueue.isEmpty());
        System.out.println(arrayQueue.peek());
    }

}
